package com.xuelang.pipeline.artifact.domain.resource.service.impl;

import com.xuelang.pipeline.artifact.client.common.result.PageRecord;
import com.xuelang.pipeline.artifact.client.common.util.BizAssert;
import com.xuelang.pipeline.artifact.client.resource.dto.RepositoryComponentQuery;
import com.xuelang.pipeline.artifact.domain.resource.RepositoryComponent;
import com.xuelang.pipeline.artifact.domain.resource.repository.RepositoryComponentRepository;
import com.xuelang.pipeline.artifact.domain.resource.service.RepositoryComponentDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 仓库制品组件领域服务实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Service
public class RepositoryComponentDomainServiceImpl implements RepositoryComponentDomainService {

    @Autowired
    private RepositoryComponentRepository repositoryComponentRepository;

    @Override
    public PageRecord<RepositoryComponent> pageList(long currentPage, long pageSize, RepositoryComponentQuery repositoryComponentQuery) {
        //BizAssert.isTrue(Nexus3V1Enum.RepositoryFormatEnum.isValid(repositoryComponentQuery.getFormat()), "组件格式不正确.取值范围为: apt,bower,cocoapods,conan,docker,gitlfs,go,helm,maven2,npm,nuget,p2,pypi,r,raw,rubygems,yum.");
        BizAssert.notNull(repositoryComponentQuery.getPackageUuid(), "制品包uuid不为空");
        return repositoryComponentRepository.pageList(currentPage, pageSize, repositoryComponentQuery);
    }

    @Override
    public List<RepositoryComponent> list(RepositoryComponentQuery repositoryComponentQuery) {
        //BizAssert.isTrue(Nexus3V1Enum.RepositoryFormatEnum.isValid(repositoryComponentQuery.getFormat()), "组件格式不正确.取值范围为: apt,bower,cocoapods,conan,docker,gitlfs,go,helm,maven2,npm,nuget,p2,pypi,r,raw,rubygems,yum.");
        BizAssert.notNull(repositoryComponentQuery.getPackageUuid(), "制品包uuid不为空");
        return repositoryComponentRepository.list(repositoryComponentQuery);
    }


    @Override
    public Boolean save(RepositoryComponent repositoryComponent) {
        return repositoryComponentRepository.save(repositoryComponent);
    }

    @Override
    public Boolean update(RepositoryComponent repositoryComponent) {
        return repositoryComponentRepository.update(repositoryComponent);
    }

    @Override
    public Boolean delete(String restId) {
        return repositoryComponentRepository.delete(restId);
    }

    @Override
    public RepositoryComponent query(String restId) {
        return repositoryComponentRepository.query(restId);
    }

    @Override
    public String queryLatestVersion(String instanceRepositoryName, String name, String group, String format) {
        List<RepositoryComponent> repositoryComponentList =  repositoryComponentRepository.listByPackage(instanceRepositoryName, name, group, format);
        if (!repositoryComponentList.isEmpty()) {
            return repositoryComponentList.get(0).getVersion();
        }
        return null;
    }

    @Override
    public void updateComponentsToOldVersion(String packageUuid) {
        repositoryComponentRepository.updateComponentsToOldVersion(packageUuid);
    }

    @Override
    public RepositoryComponent queryContainsDigestAndPushVersion(String instanceRepositoryName, String packageName, String currentDigest, String currentPushVersion, String format) {
        return repositoryComponentRepository.queryContainsDigestAndPushVersion(instanceRepositoryName, packageName, currentDigest, currentPushVersion, format);
    }

    @Override
    public String queryLatestVersionByPackageName(String instanceRepositoryName, String packageName, String format) {
        List<RepositoryComponent> repositoryComponentList =  repositoryComponentRepository.listByPackageName(instanceRepositoryName, packageName, format);
        if (!repositoryComponentList.isEmpty()) {
            return repositoryComponentList.get(0).getVersion();
        }
        return null;
    }

    @Override
    public Boolean deleteByPackageUuid(String packageUuid) {
        return repositoryComponentRepository.deleteByPackageUuid(packageUuid);
    }

}
